'use strict';

const db = uniCloud.database()

exports.main = async (event, context) => {
	const { action, data } = event
	
	switch (action) {
		case 'create':
			return await createInteractionMessage(data)
		case 'getList':
			return await getInteractionMessages(data)
		case 'markAsRead':
			return await markAsRead(data)
		case 'getUnreadCount':
			return await getUnreadCount(data)
		default:
			return {
				code: 400,
				message: '未知操作'
			}
	}
}

// 创建互动消息
async function createInteractionMessage(data) {
	const { receiver_id, sender_id, type, target_id, target_type, content } = data
	
	if (!receiver_id || !sender_id || !type || !target_id) {
		return {
			code: 400,
			message: '缺少必要参数'
		}
	}
	
	try {
		// 获取发送者信息
		const senderInfo = await db.collection('uni-id-users')
			.doc(sender_id)
			.field('nickname,avatar_file')
			.get()
		
		const sender = senderInfo.result.data[0] || {}
		
		// 创建互动消息
		const result = await db.collection('interaction-messages').add({
			receiver_id,
			sender_id,
			type,
			target_id,
			target_type: target_type || 'post',
			content: content || '',
			create_time: Date.now(),
			is_read: false,
			sender_info: {
				nickname: sender.nickname || '匿名用户',
				avatar: sender.avatar_file && sender.avatar_file.url ? sender.avatar_file.url : '/static/images/default-avatar.png'
			}
		})
		
		return {
			code: 200,
			message: '消息创建成功',
			data: result.result.id
		}
	} catch (error) {
		console.error('创建互动消息失败:', error)
		return {
			code: 500,
			message: '创建消息失败'
		}
	}
}

// 获取互动消息列表
async function getInteractionMessages(data) {
	const { receiver_id, page = 1, pageSize = 20 } = data
	
	if (!receiver_id) {
		return {
			code: 400,
			message: '缺少用户ID'
		}
	}
	
	try {
		const skip = (page - 1) * pageSize
		
		const result = await db.collection('interaction-messages')
			.where({
				receiver_id: receiver_id
			})
			.orderBy('create_time', 'desc')
			.skip(skip)
			.limit(pageSize)
			.get()
		
		return {
			code: 200,
			message: '获取成功',
			data: result.result.data
		}
	} catch (error) {
		console.error('获取互动消息失败:', error)
		return {
			code: 500,
			message: '获取消息失败'
		}
	}
}

// 标记消息为已读
async function markAsRead(data) {
	const { message_id, receiver_id } = data
	
	if (!message_id || !receiver_id) {
		return {
			code: 400,
			message: '缺少必要参数'
		}
	}
	
	try {
		await db.collection('interaction-messages')
			.doc(message_id)
			.update({
				is_read: true
			})
		
		return {
			code: 200,
			message: '标记成功'
		}
	} catch (error) {
		console.error('标记已读失败:', error)
		return {
			code: 500,
			message: '标记失败'
		}
	}
}

// 获取未读消息数量
async function getUnreadCount(data) {
	const { receiver_id } = data
	
	if (!receiver_id) {
		return {
			code: 400,
			message: '缺少用户ID'
		}
	}
	
	try {
		const result = await db.collection('interaction-messages')
			.where({
				receiver_id: receiver_id,
				is_read: false
			})
			.count()
		
		return {
			code: 200,
			message: '获取成功',
			data: result.result.total
		}
	} catch (error) {
		console.error('获取未读数量失败:', error)
		return {
			code: 500,
			message: '获取失败'
		}
	}
} 